/*
 * @lc app=leetcode.cn id=824 lang=rust
 *
 * [824] 山羊拉丁文
 */

// @lc code=start
impl Solution {
    pub fn to_goat_latin(sentence: String) -> String {
        use std::collections::HashSet;
        let mut set = HashSet::new();
        set.insert('a');
        set.insert('e');
        set.insert('i');
        set.insert('o');
        set.insert('u');

        
        let mut i = 0;
        let mut ret = String::new();
        let mut aaa = String::from("a");
        let ma = "ma";
        while i < sentence.len() {
            while i < sentence.len() && sentence.chars().nth(i).unwrap() == ' ' {
                //找到一个单词的开头
                i += 1;
            }

            let mut j = i + 1;
            while j < sentence.len() && sentence.chars().nth(j).unwrap() != ' '{
                j += 1;
            }
            //此时[i..j]是一个单词
            if set.contains(&sentence[i..i+1].to_lowercase().chars().nth(0).unwrap()){
                //元音字母开头
                ret += &sentence[i..j];
            }
            else{
                //辅音字母开头
                if i + 1 == j{
                    ret += &sentence[i..j];
                }
                else{
                    ret += &sentence[i+1..j];
                    ret += &sentence[i..i+1]
                }
            }
            ret += ma;
            ret += &aaa[..];
            ret += " ";
            aaa += "a";
            
            i = j+1;//i始终为单词开头
        }

        return String::from(&ret[0..ret.len()-1]);
    }
}
// @lc code=end

